package com.css.fxfzxczh.modules.lossAssessment.floorArea.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.entity.FloorArea;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.parm.FloorAreaParam;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.service.FloorAreaService;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/23
 * @Description: 确定总建筑面积
 */
@Api(tags = "确定总建筑面积")
@Slf4j
@RestController
@RequestMapping("/floorArea")
public class FloorAreaController {
    @Resource
    private FloorAreaService floorAreaService;

    /**
     * 分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, FloorAreaParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = floorAreaService.getPage(curPage, pageSize,param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 导入
     * @param xsemId
     * @return
     */
    @PostMapping("/reportImport")
    public RestResponse reportImport(HttpServletRequest req,String xsemId){
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = floorAreaService.reportImport(inputStream,xsemId);
            if (str==null){
                response = RestResponse.fail("导入数据中有不存在的评估子区");
            }else {
                response = RestResponse.succeed(str);
            }
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 删除单数据
     *
     * @param Id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Operation(summary = "删除数据")
    public RestResponse deleteById(@PathVariable("id") List<String> Id) {
        try {
            floorAreaService.deleteById(Id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 修改单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody FloorArea param) {
        try {
            floorAreaService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            FloorArea data = floorAreaService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @GetMapping("/getEvaluationSubarea")
    @Operation(summary = "获取评估子区下拉框")
    public RestResponse getEvaluationSubarea(String xsemId) {
        try {
            List<String> data = floorAreaService.getEvaluationSubarea(xsemId);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }
}
